package com.wanlian.service.impl;

import com.wanlian.common.PageCondition;
import com.wanlian.dao.BlackListDao;
import com.wanlian.dao.DeviceDao;
import com.wanlian.dao.WordBookDao;
import com.wanlian.entity.BlackList;
import com.wanlian.entity.Device;
import com.wanlian.entity.WordBook;
import com.wanlian.model.ResultModel;
import com.wanlian.service.BlackListServices;
import com.wanlian.service.WordBookServices;
import com.wanlian.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

@Component
public class WordBookServicesImpl implements WordBookServices {


    @Autowired
    public WordBookDao wordBookDao;


    @Override
    public ResultModel doAdd(WordBook wordBook, int userId) {
        ResultModel resultModel = new ResultModel();
        WordBook dbwordbook = wordBookDao.queryWordBookByNameId(wordBook.getWbname(), wordBook.getWbval2());
        if (dbwordbook != null) {
            resultModel.setBresult(false);
            resultModel.setMessage("该字典已存在");
            return resultModel;
        } else {
            wordBook.setUserId(userId);
            wordBook.setUpdatetime(DateUtil.currentTimestamp());
            wordBook.setEnable(true);
            wordBookDao.add(wordBook);
            resultModel.setBresult(true);
            resultModel.setMessage("添加成功");
            return resultModel;
        }
    }

    @Override
    public WordBook queryByVal(String wbval2) {
        return wordBookDao.queryWordBookByVal2(wbval2);
    }


    @Override
    public List<WordBook> queryBooksByVal(String wbval2) {
        List<WordBook> wordBooks = wordBookDao.queryWordBooksByVal2(wbval2);
        return wordBooks;
    }

    @Override
    public ResultModel doEnable(WordBook wordBook, int userId) {
        ResultModel resultModel = new ResultModel();
        WordBook wordBookdb = wordBookDao.queryById(wordBook.getId());
        if (wordBookdb == null) {
            resultModel.setMessage("该字典不存在");
            resultModel.setBresult(false);
            return resultModel;
        } else {
            String messge = "";
            if (wordBook.isEnable()) {
                messge = "启用成功";
            } else {
                messge = "禁用成功";
            }
            wordBookdb.setEnable(wordBook.isEnable());
            wordBookdb.setUserId(userId);
            wordBookdb.setUpdatetime(DateUtil.currentTimestamp());
            resultModel.setMessage(messge);
            resultModel.setBresult(true);
            return resultModel;
        }
    }


    @Override
    public ResultModel queryByCondition(PageCondition pageCondition) {
        return wordBookDao.queryWordBookList(pageCondition);
    }

    @Override
    public ResultModel doEdit(WordBook wordBook, int userId) {
        ResultModel resultModel = new ResultModel();
        WordBook wordBookdb = wordBookDao.queryWordBookFlag(wordBook);
        if (wordBookdb != null) {
            resultModel.setBresult(false);
            resultModel.setMessage("修改失败，该字典值已存在");
            return resultModel;
        } else {
            WordBook wordBook1 = wordBookDao.queryById(wordBook.getId());
            wordBook.setUserId(userId);
            wordBook.setUpdatetime(DateUtil.currentTimestamp());
            if (wordBook.getWbname().equals("") || wordBook.getWbname().equals(null)) {
                resultModel.setMessage("名称不能为空");
                return resultModel;
            } else {
                wordBook1.setWbname(wordBook.getWbname());
            }
            if (wordBook.getWbtype().equals("") || wordBook.getWbname().equals(null)) {
                resultModel.setMessage("类型不能为空");
                return resultModel;
            } else {
                wordBook1.setWbtype(wordBook.getWbtype());
            }
            if (wordBook.getWbval1().equals("") || wordBook.getWbval1().equals(null)) {
                resultModel.setMessage("有效值1不能为空");
                return resultModel;
            } else {
                wordBook1.setWbval1(wordBook.getWbval1());
            }
            if (wordBook.getWbval2().equals("") || wordBook.getWbval2().equals(null)) {
                resultModel.setMessage("有效值2不能为空");
                return resultModel;
            } else {
                wordBook1.setWbval2(wordBook.getWbval2());
            }


            wordBook1.setRemark(wordBook.getRemark());
            wordBookDao.update(wordBook1);
            resultModel.setBresult(true);
            resultModel.setMessage("修改成功");
            return resultModel;
        }

    }


    @Override
    public ResultModel queryById(int id) {
        WordBook wordBook = wordBookDao.queryById(id);
        ResultModel resultMode = new ResultModel();
        resultMode.setBresult(true);
        resultMode.setObject(wordBook);
        return resultMode;
    }

    @Override
    public ResultModel queryChannel() {
        ResultModel resultMode = new ResultModel();
        resultMode.setBresult(true);
        resultMode.setObject(wordBookDao.queryChannel());
        return resultMode;
    }
}
